﻿using System;
using System.Collections.Generic;
using PoolGame2.Physics;
using PoolGame2.Rules;

namespace PoolGame2.Game
{
    public class GameRules : RuleC
    {
        PoolGame Game;
        Ball8Rules Ball8;
        BallCRules BallC;
        BallRules BallB;

        public GameRules(PoolGame game)
        {
            this.Game = game;
            Ball8 = new Ball8Rules(Game);
            BallC = new BallCRules(Game);
            BallB = new BallRules(Game);
        }

        public RuleTasks runRules(Ball ball)
        {
            List<RulesEnum> rules = new List<RulesEnum>();
            if (ball.getNumber() != 0 && ball.getNumber() != 8 && ball.getPocketed())
            {
                //Standard ball pocket
                rules.Add(BallB.CheckRules(ball));
            }
            else if (ball.getNumber() == 0 && ball.getPocketed())
            {
                rules.Add(BallC.CheckRules(ball));
            }
            else if (ball.getNumber() == 8 && ball.getPocketed())
            {
                rules.Add(Ball8.CheckRules(ball));
            }

            if (rules.Contains(RulesEnum.ERROR))
            {
                // Oh we're in trouble now...
                throw new SystemException();
            }
            else if (rules.Contains(RulesEnum.POT_WIN))
            {
                // Game over, player wins!
                return RuleTasks.WIN_GAME;
            }
            else if (rules.Contains(RulesEnum.POT_LOSE) && Game.gameManager.GetBreak())
            {
                // Rerack
                return RuleTasks.RERACK;
            }
            else if (rules.Contains(RulesEnum.POT_LOSE) && !Game.gameManager.GetBreak())
            {
                // Game over, player loses!
                return RuleTasks.KILL_GAME;
            }
            else if (rules.Contains(RulesEnum.POT_INVALID))
            {
                // Player's made a mistake
                return RuleTasks.TWO_SHOTS;
            }
            else if (rules.Contains(RulesEnum.POT_COLOUR_FIRST))
            {
                return RuleTasks.CHOOSE_COLOUR;
            }
            else if (rules.Contains(RulesEnum.POT_STRIPE_FIRST))
            {
                return RuleTasks.CHOOSE_STRIPES;
            }
            else if (rules.Contains(RulesEnum.POT_CUE))
            {
                return RuleTasks.RESET_CUEBALL;
                //TODO: Check if the player's on black?
                // Do POT_INVALID & reset cueball
            }
            else if (rules.Contains(RulesEnum.POT_VALID))
            {
                return RuleTasks.POT_VALID;
            }else{
                return RuleTasks.OK;
                // All ok it seems!
            }
        }
        public RuleTasks runRules(Ball ball1, Ball ball2)
        {
            List<RulesEnum> rules = new List<RulesEnum>();
            if (ball1.getNumber() == 0)
            {
                if (BallC.CheckRules(ball1, ball2) == RulesEnum.SHOT_INVALID)
                {
                    // Shots+2
                    return RuleTasks.TWO_SHOTS;
                }
                else if (BallC.CheckRules(ball1, ball2) == RulesEnum.ERROR)
                {
                    // Uh oh...
                    throw new SystemException();
                }
            }
            return RuleTasks.OK;
        }

        public override RulesEnum CheckRules(Ball ball)
        {
            throw new NotImplementedException();
        }
        public override RulesEnum CheckRules(Ball ball1, Ball ball2)
        {
            throw new NotImplementedException();
        }
    }
}
